//
//  ViewController.m
//  首行选中demo
//
//  Created by on 2017/4/25.
//  Copyright © 2017年 All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *myTableView;
    NSMutableArray *dataArray;
 
    NSIndexPath *lastIndex;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    dataArray = [[NSMutableArray alloc] init];
    for (int i=0; i<10; i++)
    {
        [dataArray addObject:@(0)];
    }
    //lastIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    //[dataArray replaceObjectAtIndex:0 withObject:@(1)];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];
 
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",dataArray[indexPath.row]];
    if (indexPath.row==0)
    {
        if (lastIndex != nil)
        {
            if (lastIndex.row == indexPath.row)
            {
                cell.textLabel.textColor = [UIColor redColor];
            }
            else
            {
                cell.textLabel.textColor = [UIColor blackColor];
            }
        }
        else
        {
            cell.textLabel.textColor = [UIColor blackColor];
        }
        
    }
    else
    {
        if (lastIndex.row == indexPath.row)
        {
            cell.textLabel.textColor = [UIColor redColor];
        }
        else
        {
            cell.textLabel.textColor = [UIColor blackColor];
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != lastIndex.row)
    {
        //上个cell
        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
        lastCell.textLabel.textColor = [UIColor blackColor];
        //当前cell
        UITableViewCell *currentCell = [tableView cellForRowAtIndexPath:indexPath];
        currentCell.textLabel.textColor = [UIColor redColor];
        lastIndex = indexPath;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
